import java.util.*;

/**
 * @Author: Aerospace
 * @Date: 2021/4/20 19:44
 * @Description: Dream begins
 */
public class ArrayContinuousValue {

    public Integer maxArea(List<List<Integer>> a){
        //如果数组a为空直接返回0个
        if (a.size()<1) return 0;
        int result = 0;
        //如果数组a包含节点个数等于1
        if (a.size() == 1){
            result = getMaxFromOneDirectionList(a.get(0));
            return result;
        }
        //如果数组a包含节点个数大于1
        result = getMaxFromTwoDirectionList(a);
        return result;
    }

    private int getMaxFromTwoDirectionList(List<List<Integer>> a) {
//        Map<Integer, Integer> tempMap = new HashMap<>();
        int result = 0;
        int al = a.size();
        for (int i = 0; i < al; ) {
            for (int j = i; j<a.get(i).size(); ){
                Set<String> tempSet = new HashSet<>();
                int temp = recursionOnePoint(a,i,j,0,tempSet);
//                System.out.println("i:"+i+",j:"+j+",temp:"+temp);
                if (temp>result) result = temp;
                j++;
            }
            i++;
        }
        return result;
    }

    public int recursionOnePoint(List<List<Integer>> a, int i, int j, int result, Set<String> tempSet) {
        if (Check(a, i , j, tempSet)) return 0;
        result += countOne(a, i, j, 0, tempSet);
        if (i-1>=0&&j<a.get(i-1).size()&&a.get(i-1).get(j)>0){
//            System.out.println("上"+(i-1)+","+j);
            result +=recursionOnePoint(a,i-1,j,0, tempSet);
//            System.out.println(result);
        }
        if (j-1>=0&&a.get(i).get(j-1)>0){
//            System.out.println("左"+i+","+(j-1));
            result +=recursionOnePoint(a,i,j-1,0, tempSet);
//            System.out.println(result);
        }
        if (i+1<a.size()&&j<a.get(i+1).size()&&a.get(i+1).get(j)>0){
//            System.out.println("下"+(i+1)+","+j);
            result +=recursionOnePoint(a,i+1,j,0, tempSet);
//            System.out.println(result);
        }
        if (j+1<a.get(i).size()&&a.get(i).get(j+1)>0){
//            System.out.println("右"+i+","+(j+1));
            result +=recursionOnePoint(a,i,j+1,0, tempSet);
//            System.out.println(result);
        }
        return result;
    }


    private int countOne(List<List<Integer>> a, int i, int j, int result, Set<String> tempSet) {
        if (a.get(i).get(j)>0){
            tempSet.add(i+","+j);
            result++;
        }
        return result;
    }

    private boolean Check(List<List<Integer>> a, int i, int j, Set<String> tempSet) {
        if (a.get(i).get(j)==0){
            return true;
        }else if (a.get(i).get(j)>0){
            if (tempSet.contains(i+","+j)){
                return true;
            }
        }
        return false;
    }



    private int getMaxFromOneDirectionList(List<Integer> integers) {
        int result = 0;
        for (Integer b:integers){
            if (b>0) result++;
            if (b==0) result = 0;
        }
        return result;
    }

}
